package one.stand.service.impl;

import com.github.pagehelper.PageInfo;
import one.stand.model.CompanyCouponModel;
import one.stand.model.ResultModel;
import one.stand.request.CompanyCouponRequest;
import one.stand.response.CompanyCouponResponse;
import one.stand.service.CompanyCouponService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: QIK
 * @CreateDate: 2019/6/24 15:19
 */
@Service
public class CompanyCouponServiceImpl extends PublicBaseService implements CompanyCouponService {
    @Override
    public ResultModel<Boolean> add(CompanyCouponRequest request) {
        if(null==request.getCouponType()
                ||null==request.getEffectiveStartTime()
                ||null==request.getEffectiveEndTime()
                ||null==request.getGrantEndTime()
                ||null==request.getGrantStartTime()
                ||null==request.getCouponParam()
                ||null==request.getSatisfyMoney()
                ||null==request.getCouponNum()
                ||null==request.getReceiveNum()
                ||null==request.getGrantMode()
                ||null==request.getGrantMode()
        ){
            return ResultModel.checkParamFail();
        }
        CompanyCouponModel companyCouponModel=new CompanyCouponModel();
        BeanUtils.copyProperties(request,companyCouponModel);
        companyCouponModel.setCompanyId(request.getCompanyId());
       int insertRes= companyCouponMapper.insertSelective(companyCouponModel);
        if(insertRes<=0){
            return ResultModel.fail();
        }
        return ResultModel.success();
    }

    @Override
    public ResultModel<Boolean> upd(CompanyCouponRequest request) {
        if(null==request.getCouponId()||(null==request.getEffectiveEndTime()&&null==request.getGrantEndTime())){
            return ResultModel.checkParamFail();
        }
        CompanyCouponModel companyCouponModel=companyCouponMapper.selectByPrimaryKey(request.getCouponId());
        if(null==companyCouponModel){
            return ResultModel.noData();
        }
        if(null!=request.getEffectiveEndTime()){
            if(request.getEffectiveEndTime().getTime() <= companyCouponModel.getEffectiveEndTime().getTime()){
                return ResultModel.fail("只能延长有效期！！！");
            }
            companyCouponModel.setEffectiveEndTime(request.getEffectiveEndTime());
        }
        if(null!=request.getGrantEndTime()){
            if(request.getGrantEndTime().getTime()<=companyCouponModel.getGrantEndTime().getTime()){
                return ResultModel.fail("不能缩短发放截止时间！！！");
            }
            companyCouponModel.setGrantEndTime(request.getGrantEndTime());
        }
        int updRes=companyCouponMapper.updateByPrimaryKeySelective(companyCouponModel);
        if(updRes<=0){
            return ResultModel.fail();
        }
        return ResultModel.success();
    }

    @Override
    public ResultModel<Boolean> del(CompanyCouponRequest request) {
        if(null==request.getCouponId()){
            return ResultModel.checkParamFail();
        }
        CompanyCouponModel companyCouponModel=companyCouponMapper.selectByPrimaryKey(request.getCouponId());
        if(null==companyCouponModel){
            return ResultModel.noData();
        }
        if(companyCouponModel.getEffectiveEndTime().getTime()<=currentDate().getTime()){
            return ResultModel.fail("只能删除过期的优惠券");
        }
        int delRes=companyCouponMapper.deleteByPrimaryKey(request.getCouponId());
        if(delRes<=0){
            return ResultModel.fail();
        }
        return ResultModel.success();

    }

    @Override
    public ResultModel<PageInfo<CompanyCouponResponse>> crmPcPageList(CompanyCouponRequest request) {
        CompanyCouponModel model=new CompanyCouponModel();
        BeanUtils.copyProperties(request,model);
        model.setCompanyId(request.getCId().intValue());
        List<CompanyCouponModel> list=companyCouponMapper.crmPcSelectGoodsTypeList(model);
        List<CompanyCouponResponse> companyCouponResponses=new ArrayList<>(0);
        list.forEach(l->{{
            CompanyCouponResponse response=new CompanyCouponResponse();
            BeanUtils.copyProperties(l,response);
            companyCouponResponses.add(response);
        }});
        PageInfo<CompanyCouponResponse> pageInfo = new PageInfo(companyCouponResponses);
        return ResultModel.success(pageInfo);
    }
}
